package com.ysx.dfs.controller;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.ysx.common.pojo.Result;
import com.ysx.common.utils.GreenImageScan;
import com.ysx.common.utils.GreenTextScan;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dfs")
@Slf4j
public class PicController {

    @Autowired
    protected FastFileStorageClient fastFileStorageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Autowired
    private GreenImageScan greenImageScan;
    @Autowired
    private GreenTextScan greenTextScan;

    //上传图片
    @PostMapping("/upload")
    public Result<Map<String,String>> uploadPic(@RequestBody MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();//获得源文件名
        String shuFixName = filename.substring(filename.lastIndexOf(".") + 1);
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(),file.getSize(),shuFixName,null);
        String fullName = storePath.getFullPath();
        String fullPath = fdfsWebServer.getWebServerUrl() + fullName;//完整路径
//        String path = storePath.getPath();
//        String group = storePath.getGroup();
        Map<String,String> map=new HashMap<>();
        //=======================测试阿里云内容安全功能
        byte[] imagesByte = file.getBytes();
        List<byte[]> bytes = Arrays.asList(imagesByte);
        Map imagesMapResult = greenImageScan.imageScan(bytes);
        log.debug("图片审核结果：{}",imagesMapResult);
        String text="卖冰毒了,5块钱一斤买不了吃亏买不了上当";
        List<String> stringList = Arrays.asList(text);
        Map textMapResult = greenTextScan.greeTextScan(stringList);
        log.debug("文字审核结果：{}",textMapResult);
        //======================测试完毕
        map.put("url",fullPath);//后期扩展需要
        return Result.ok(map);
    }



}
